'''
@Project ：apiAutoTest
@File    ：deps.py
@IDE     ：PyCharm
@Author  ：朱广杰
@Date    ：2023/11/24 18:35
'''


import typing

from sqlalchemy.ext.asyncio import AsyncSession
from starlette.requests import Request
from db.redis_db import MySyncRedis
from db.mysql_db import async_session


async def get_db() -> typing.AsyncGenerator[AsyncSession, None]:
    """ sql连接会话 """
    async with async_session() as session:
        try:
            yield session
            await session.commit()
        except Exception:
            await session.rollback()
            raise
        finally:
            await session.close()


async def get_redis(request: Request) -> MySyncRedis:
    """ redis连接对象 """
    return await request.app.state.redis
